package com.shuqi.bookcontent;

import java.util.HashMap;

import android.graphics.Paint;
import android.graphics.Typeface;

/***
 * 
 * 维护绘制过程中的画笔的状态.
 * 
 * @author wyu
 * 
 */
public class BookContentPaint extends Paint {


    private HashMap<String, IBookContentPaintStyle> mStyleCache =
            new HashMap<String, IBookContentPaintStyle>();

    public interface IBookContentPaintStyle {

        BOOK_CONTENT_PAIN_TYPE getStyleType();

        int getTextColor();

        int getTextSize();

        Typeface getTypeface();
    }

    public static enum BOOK_CONTENT_PAIN_TYPE {
        PAY_TITLE, CONTENT, CONTENT_TITLE, TOP, BOTTOM, PAY_BUTTON, PAY_TIP
    }

    private BOOK_CONTENT_PAIN_TYPE mPaintType;

    public BookContentPaint() {
        super(Paint.ANTI_ALIAS_FLAG);
        setTextAlign(Align.LEFT);
    }

    private void resetPaint(BOOK_CONTENT_PAIN_TYPE type) {

        if (mPaintType == type)
            return;

        mPaintType = type;

        IBookContentPaintStyle sStyle = mStyleCache.get(type.toString());
        if (sStyle == null) {
            sStyle = generStyle(type);
            mStyleCache.put(type.toString(), sStyle);
        }

        setColor(sStyle.getTextColor());
        setTextSize(sStyle.getTextSize());
        setTypeface(sStyle.getTypeface());
    }

    private IBookContentPaintStyle generStyle(BOOK_CONTENT_PAIN_TYPE type) {
        IBookContentPaintStyle sStyle = null;
        switch (type) {
            case CONTENT:
                sStyle = new PaintContentStyle();
                break;
            case PAY_TITLE:
                sStyle = new PaintPayTitleStyle();
                break;
            case BOTTOM:
                sStyle = new PaintBottomStyle();
                break;
            case TOP:
                sStyle = new PaintTopStyle();
                break;
            case PAY_BUTTON:
                sStyle = new PaintButtonTextStyle();
                break;
            case PAY_TIP:
                sStyle = new PaintPayTipStyle();
                break;
            case CONTENT_TITLE:
                sStyle = new PaintContentTitleStyle();
                break;
            default:
                break;
        }

        return sStyle;
    }

    public void changeStyleToContent() {
        resetPaint(BOOK_CONTENT_PAIN_TYPE.CONTENT);
    }

    public void changeStyleToContentTitle() {
        resetPaint(BOOK_CONTENT_PAIN_TYPE.CONTENT_TITLE);
    }

    public void changeStyleToPayTitle() {
        resetPaint(BOOK_CONTENT_PAIN_TYPE.PAY_TITLE);
    }

    public void changeStyleToTop() {
        resetPaint(BOOK_CONTENT_PAIN_TYPE.TOP);
    }

    public void changeStyleToBottom() {
        resetPaint(BOOK_CONTENT_PAIN_TYPE.BOTTOM);
    }

    public void changeStyleToPayButton() {
        resetPaint(BOOK_CONTENT_PAIN_TYPE.PAY_BUTTON);
    }

    public void changeStyleToPayTip() {
        resetPaint(BOOK_CONTENT_PAIN_TYPE.PAY_TIP);
    }

    public void release() {
        if (mStyleCache != null) {
            mStyleCache.clear();
        }
    }
}
